package com.bdmc.web.controller.base;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bdmc.base.domain.bo.BaseCarBo;
import com.bdmc.base.domain.vo.BaseCarVo;
import com.bdmc.base.service.IBaseCarService;
import com.bdmc.base.service.IBaseDeviceService;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 车辆信息控制器
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/car")
public class BaseCarController extends BaseController {

    private final IBaseCarService iBaseCarService;

    private final IBaseDeviceService deviceService;

    /**
     * 查询车辆信息列表
     */
    @SaCheckPermission("base:car:list")
    @GetMapping("/list")
    public TableDataInfo<BaseCarVo> list(@Validated(QueryGroup.class) BaseCarBo bo, PageQuery pageQuery) {
        return iBaseCarService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出车辆信息列表
     */
    @SaCheckPermission("base:car:export")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BaseCarBo bo, HttpServletResponse response) {
        List<BaseCarVo> list = iBaseCarService.queryList(bo);
        ExcelUtil.exportExcel(list, "车辆信息", BaseCarVo.class, response);
    }

    /**
     * 获取车辆信息详细信息
     */
    @SaCheckPermission("base:car:query")
    @GetMapping("/{carId}")
    public AjaxResult<BaseCarVo> getInfo(@PathVariable("carId") Long carId) {
        return AjaxResult.success(iBaseCarService.queryById(carId));
    }

    /**
     * 新增车辆信息
     */
    @SaCheckPermission("base:car:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BaseCarBo bo) {
        return toAjax(iBaseCarService.insertByBo(bo));
    }

    /**
     * 修改车辆信息
     */
    @SaCheckPermission("base:car:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BaseCarBo bo) {
        return toAjax(iBaseCarService.updateByBo(bo));
    }

    /**
     * 绑定车辆信息
     */
    @SaCheckPermission("base:car:bind")
    @Log(title = "绑定车辆信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("bind")
    public AjaxResult<Void> bind(@Validated(EditGroup.class) @RequestBody BaseCarBo bo) {
        return toAjax(iBaseCarService.bindDevice(bo));
    }

    @SaCheckPermission("base:car:bind")
    @Log(title = "绑定车辆信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("unbind")
    public AjaxResult<Void> unbind(@Validated(EditGroup.class) @RequestBody BaseCarBo bo) {

        return toAjax(iBaseCarService.unbindDevice(bo));
    }

    /**
     * 删除车辆信息
     */
    @SaCheckPermission("base:car:remove")
    @Log(title = "车辆信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{carIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] carIds) {
        return toAjax(iBaseCarService.deleteWithValidByIds(Arrays.asList(carIds), true));
    }

    @GetMapping("combo")
    public AjaxResult<List<BaseCarVo>> combo(BaseCarBo bo){
        return AjaxResult.success(iBaseCarService.queryList(bo));
    }
}
